package com.feizi.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.feizi.entity.AddressEntity;
import com.feizi.entity.CommentEntity;
import com.feizi.entity.OrderdetailEntity;
import com.feizi.entity.OrderinfoEntity;
import com.feizi.entity.ShopcartEntity;
import com.feizi.entity.UserEntity;
import com.feizi.service.AddressService;
import com.feizi.service.CommentService;
import com.feizi.service.OrderService;
import com.feizi.service.ShopcartService;
@Controller
public class OrderController {
	
	@Autowired
	private ShopcartService shopcartService;
	@Autowired
	private AddressService addressService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private CommentService commentService;
	/**
	 * 查询
	 * @param session
	 * @param model
	 * @param ids
	 * @return
	 */
	@RequestMapping(path="/toOrder.do",method=RequestMethod.GET)
	public String toOrder(HttpSession session,Model model,String ids) {
		
		UserEntity user = (UserEntity) session.getAttribute("user");
		List<AddressEntity> address=addressService.findAddress(user.getUserid());
		List<ShopcartEntity> shipcart=shopcartService.getShopcart(ids);
		double total=0;
		for (ShopcartEntity s : shipcart) {
			total+=s.getTotalprice();
		}
		model.addAttribute("address", address);
		model.addAttribute("shopcart", shipcart);
		model.addAttribute("total", total);
		model.addAttribute("ids", ids);
		return "order";
	}
	/**
	 * 查询订单
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping(path="/findGoods.do",method=RequestMethod.GET)
	public String findGoods(HttpSession session,Model model,String orderid) {
		OrderinfoEntity oie=new OrderinfoEntity();
		UserEntity user = (UserEntity) session.getAttribute("user");
		oie.setF_uid(user.getUserid());
		oie.setOrderid(orderid);
		List<OrderinfoEntity> orderinfo=orderService.findOrderif(oie);
		for (OrderinfoEntity o : orderinfo) {
			o.setOrderdetail(orderService.findOrderdetail(o.getOrderid()));
		}
		model.addAttribute("orderinfo", orderinfo);
		return "orders";
	}
	/**
	 * 生成订单
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping(path="/createOrder.do",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> createOrder(HttpSession session,String ids,int addressId) {
		UserEntity user = (UserEntity) session.getAttribute("user");
		
		//查询当前用户的购物车所勾选的数据
		List<ShopcartEntity> shipcart=shopcartService.getShopcart(ids);
		
		//获取收货地址
		AddressEntity address=addressService.findOrAddress(addressId);
		
		//计算总价
		double total=0;
		for (ShopcartEntity s : shipcart) {
			total+=s.getTotalprice();
		}
		
		//生成订单号
		SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMddHHmmss");
		String format=dateFormat.format(new Date());
		String orderid="699"+format+"00";
		
		//插入订单主表数据（待付款）
		OrderinfoEntity oie=new OrderinfoEntity();
		oie.setOrderid(orderid);
		oie.setF_uid(user.getUserid());
		oie.setStatus(0);
		oie.setTotalprices(total);
		oie.setName(address.getName());
		oie.setPhone(address.getPhone());
		oie.setProvince(address.getProvince());
		oie.setCity(address.getCity());
		oie.setArea(address.getArea());
		oie.setAddressinfo(address.getAddressInfo());
		orderService.addOrderinfo(oie);
		
		//插入订单明细表数据
		OrderdetailEntity ode=new OrderdetailEntity();
		ode.setF_orderid(orderid);
		for(ShopcartEntity c:shipcart) {
			ode.setCount(c.getCount());
			ode.setF_pid(c.getF_pid());
			ode.setPrice(c.getPrice());
			ode.setSize(c.getSize());
			ode.setTotalprice(c.getTotalprice());
			orderService.addOrderdetail(ode);
		}
		
		//将当前用户的购物车所勾选的产品删除
		shopcartService.delOrShopcart(ids);
		
		//跳转到二维码页面
		Map<String, Object> rslMap=new HashMap<String, Object>();
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("orderid", orderid);
		map.put("totalprices", total);
		map.put("userid", user.getUserid());
		rslMap.put("code","1");
		rslMap.put("data", map);
		rslMap.put("msg","生成订单成功");
		return rslMap;
	}
	/**
	 * 确认收货
	 * @param session
	 * @param orderid
	 * @return
	 */
	@RequestMapping(path="/upGoods.do",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> upGoods(HttpSession session,String orderid) {
		int rows=orderService.upGoods(orderid);
		Map<String, Object> rslMap=new HashMap<String, Object>();
		rslMap.put("code",rows>0?"1":"0");
		rslMap.put("msg",rows>0? "收货成功" : "收货失败");
		return rslMap;
	}
	/**
	 * 评价
	 * @param session
	 * @param comment
	 * @param orderid
	 * @param id
	 * @return
	 */
	@RequestMapping(path="/addComment.do",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addComment(HttpSession session,CommentEntity comment,String orderid,int id) {
		UserEntity user = (UserEntity) session.getAttribute("user");
		comment.setF_uid(user.getUserid());
		//评价
		int rows=commentService.addComment(comment);
		//修改订单详情里的是否评价
		orderService.upOdStatus(id);
		//查看订单里未评价的数量
		int counts=orderService.findOdStatus(orderid);
		//修改订单主表状态
		if(counts == 0) {
			orderService.upEstimate(orderid);
		}
		Map<String, Object> rslMap=new HashMap<String, Object>();
		rslMap.put("code",rows>0?"1":"0");
		rslMap.put("msg",rows>0? "评论成功" : "评论失败");
		return rslMap;
	}
}
